home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #1 / Amiga Plus 1999 #1.iso / System-Boost / Workbench / BackClock / sources / utils.c < prev    next >
C/C++ Source or Header  |  1998-08-10  |  10KB  |  327 lines

  1. /*****************************************************************************
  2.  * 
  3.  * Nom                          : utils.c
  4.  * desc                         : utilitaires projet
  5.  *
  6.  * version                      : $VER: utils.c 2.2ß2 (14.07.98)
  7.  *
  8.  * ver 2.1:   error handling
  9.  * ver 2.2:   some bug fixed
  10.  * ver 2.2ß2: stay in the background now
  11.  * 
  12.  *
  13.  *****************************************************************************
  14.  */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include <exec/exec.h>
  20. #include <exec/ports.h>
  21. #include <exec/io.h>
  22. #include <exec/tasks.h>
  23. #include <exec/libraries.h>
  24. #include <dos/datetime.h>
  25. #include <dos/dosextens.h>
  26. #include <workbench/workbench.h>
  27. #include <libraries/notifyintuition.h>
  28. #include <cybergraphx/cybergraphics.h>
  29.  
  30. #include <proto/exec.h>
  31. #include <proto/intuition.h>
  32. #include <proto/dos.h>
  33. #include <proto/graphics.h>
  34. #include <proto/gadtools.h>
  35. #include <proto/icon.h>
  36. #include <proto/cybergraphics.h>
  37. #include "utils.h"
  38. #include "timer.h"
  39. #include "tracewin.h"
  40. #include "notify.h"
  41. #include "partial.h"
  42. #include "conf.h"
  43. #include "obp.h"
  44.  
  45. struct Library * NotifyIntuitionBase ;
  46. struct Library * CyberGfxBase ;
  47.  
  48. char * ptrTime = NULL ;
  49. struct DateTime * dt = NULL ;
  50.  
  51. idWin * init(WBArg * appName) {
  52.   /* initalize the main window
  53.    * and the project
  54.    */
  55.   struct Screen  * WBScreen = NULL ;                 // lock on WB
  56.   idWin * prjWin = NULL ;                            // main prj
  57.   
  58.   
  59.   /* do we use CGX ?
  60.    */
  61.   CyberGfxBase = OpenLibrary(CYBERGFXNAME, 40L) ;
  62.   
  63.   if ((prjWin = AllocVec(sizeof(idWin), MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  64.     return(NULL) ;
  65.     
  66.     
  67.   if ((prjWin->date = AllocVec(sizeof(struct DateStamp), MEMF_PUBLIC)) == NULL) {
  68.     close(prjWin) ;
  69.     return(NULL) ;
  70.   }
  71.     
  72.   if ((prjWin->Notify = AllocVec(sizeof(struct IntNotifyRequest), MEMF_PUBLIC|MEMF_CLEAR)) == NULL) {
  73.     close(prjWin) ;
  74.     return(NULL) ;
  75.   }
  76.   
  77.   
  78.   if ((NotifyIntuitionBase = OpenLibrary(NOTIFYINTUITIONNAME, NOTIFYINTUITION_VMIN)) != NULL) {
  79.         /* open the window
  80.          */
  81.     LoadConf(prjWin) ;
  82.     WBScreen = LockPubScreen("Workbench") ;
  83.     top = WBScreen->BarHeight ; 
  84.     prjWin->wb   = WBScreen ;
  85.     init_bitmap(prjWin) ;
  86.     prjWin->win = OpenWindowTags(NULL, WA_Left,   prjWin->backWin.posX,
  87.                                        WA_Top,         prjWin->backWin.posY,
  88.                                        WA_Width,       prjWin->backWin.width,
  89.                                        WA_Height,      prjWin->backWin.height,
  90.                                        WA_IDCMP,       IDCMP,
  91.                                        WA_MinWidth,     50,
  92.                                        WA_MinHeight,   50,
  93.                                        WA_MaxHeight,   MAXH,
  94.                                        WA_MaxWidth,    MAXH,
  95.                                        WA_Flags,       WFLG,
  96.                                        WA_ScreenTitle, TXT_SCRTITLE,
  97.                                        WA_NewLookMenus, TRUE,
  98.                                        WA_PubScreenName, "Workbench", TAG_DONE) ;
  99.     
  100.     szgdg.Height = top - 1 ;
  101.     szgdg.NextGadget = &tagdg ;
  102.     tagdg.LeftEdge  = prjWin->win->Width - 10 ;
  103.     tagdg.TopEdge   = prjWin->win->Height - 10 ;
  104.     tagdg.Width     = 10 ;
  105.     tagdg.Height    = 10 ;
  106.   
  107.     AddGList(prjWin->win, &szgdg, 0, 2, NULL) ;
  108.    
  109.     RefreshGList(&szgdg, prjWin->win, NULL, -1) ;
  110.                                                                                              
  111.     if (prjWin->win == NULL)
  112.       return(NULL) ;
  113.    
  114.     if (prjWin->wb)
  115.       UnlockPubScreen(NULL, WBScreen) ;
  116.     
  117.                                                                                                 
  118.           // initialisation ...  (fenetre...)
  119.   }else {
  120.     close(prjWin) ;
  121.     return(NULL) ;
  122.   }
  123.   initwin(prjWin) ;
  124.   if (!initTimer(prjWin))   { close(prjWin) ; return(NULL) ;}
  125.   if (!startNotify(prjWin)) { close(prjWin) ; return(NULL) ;}
  126.   if ((ptrTime = AllocVec(LEN_DATSTRING, MEMF_PUBLIC|MEMF_CLEAR)) == NULL) {close(prjWin) ; return(NULL) ;}
  127.   if ((dt =      AllocVec(sizeof(struct DateTime),  MEMF_PUBLIC|MEMF_CLEAR)) == NULL) {close(prjWin) ; return(NULL) ;}
  128.   return prjWin ;
  129. }
  130.  
  131. void close(idWin * prj) {
  132.   int i;
  133.   if (prj) {                              // struct prj existe (<>0) 
  134.     endNotify(prj) ;
  135.     if (prj->win) CloseWindow(prj->win) ; // ferme la fenetre
  136.     free_twin(prj) ; 
  137.     if (prj->date) FreeVec(prj->date) ;   // libere datestamp 
  138.     if (prj->Notify) FreeVec(prj->Notify) ; // libere struct IntNotify
  139.     closeTimer(prj) ;
  140.     
  141.     if (NotifyIntuitionBase) CloseLibrary(NotifyIntuitionBase) ;
  142.     prj->wb = LockPubScreen("Workbench") ;
  143.     for(i = 0; i<NUM_COLORS; i++)
  144.       freePen(prj, prj->backWin.cmap[i].reg) ;
  145.     if (prj->wb) UnlockPubScreen(NULL, prj->wb) ;
  146.     if (ptrTime) FreeVec(ptrTime) ;
  147.     free_bitmap(prj) ;
  148.     FreeVec(prj) ;                        // libere la memoire
  149.     FreeVec(dt) ;
  150.     if (CyberGfxBase) CloseLibrary(CyberGfxBase) ;
  151.   }
  152. }
  153. void getDate(idWin * prj) {
  154.   // remplit la structure (projet) avec la date
  155.    
  156.   DateStamp(prj->date) ;
  157.   if (ptrTime) {  
  158.     CopyMem(prj->date, &(dt->dat_Stamp), sizeof(struct DateStamp)) ;
  159.     dt->dat_Format = FORMAT_DOS ;
  160.     dt->dat_StrTime = ptrTime ;
  161.     DateToStr(dt) ;
  162.     //CopyMem(ptrTime, prj->datestr, LEN_DATSTRING) ;
  163.     prj->oldH = prj->heu ;
  164.     prj->oldM = prj->min ;
  165.     prj->oldS = prj->sec ;
  166.     DateToByte(ptrTime, prj) ;
  167.   
  168.   }
  169. }
  170.  
  171. void processwin(idWin* prj) {
  172.   /* variables
  173.    */
  174.   ULONG signals = NULL,                                                                          /* masque des signaux a attendre */
  175.         mask,
  176.         class,                                                                                                                   /* masque des signaux recus */
  177.         sigtimer ;
  178.         
  179.   BOOL  stop    = FALSE ;                                                                                                                /* vrai si ctrl c */
  180.   struct IntuiMessage * msg ;                     /* ptr sur message intuition */
  181.   struct MsgPort      * extPort ;
  182.   struct backMsg      * extmsg ;
  183.   
  184.   extPort = CreateMsgPort() ;
  185.   extPort->mp_Node.ln_Name = AllocVec(10, MEMF_PUBLIC) ;
  186.   CopyMem("backclock", extPort->mp_Node.ln_Name, 10) ;
  187.   AddPort(extPort) ;
  188.   
  189.   sigtimer =  (1<<(prj->treq->tr_node.io_Message.mn_ReplyPort->mp_SigBit)) ; 
  190.   /* signaux d'attentes
  191.    */
  192.   signals = SIGBREAKF_CTRL_C | 
  193.             (1<<(prj->notifyPort->mp_SigBit)) | 
  194.             (1<<(prj->win->UserPort->mp_SigBit)) | sigtimer|
  195.             (1<<(extPort->mp_SigBit)) ;
  196.   
  197.   runtimer(prj) ;
  198.   getDate(prj) ;
  199.   retracer(prj) ;
  200.   while(!stop) {
  201.     mask = Wait(signals) ;
  202.     if (mask & SIGBREAKF_CTRL_C) {
  203.       /* recu le signal CTRL C
  204.        */
  205.       stop=TRUE;
  206.     }
  207.     if (mask & (1<<(extPort->mp_SigBit))) {
  208.       // recu message prog externe
  209.       extmsg = (struct backMsg *)GetMsg(extPort) ;
  210.       switch(extmsg->Class) {
  211.         case BC_Quit:        // recu demande d'arret
  212.           stop=TRUE ;
  213.           extmsg->error = OK ;
  214.           break;
  215.       
  216.         case BC_GetPrj:      // recu demande d'envoi du projet
  217.           extmsg->ptrPrj = prj ;
  218.           extmsg->error = OK ;
  219.           break;
  220.       
  221.         case BC_RefreshColors:
  222.           setColors(prj) ;
  223.           break;
  224.       
  225.         case BC_Refresh:     // refresh window (redraw)
  226.           reinit_win(prj) ;
  227.           getDate(prj) ;
  228.           effacer(prj) ;
  229.           retracer(prj) ;
  230.           extmsg->error = OK ;
  231.           break;
  232.       
  233.         case BC_SaveConf:
  234.           SaveEnv(prj, TRUE) ;
  235.           if (prj->lastError) extmsg->error = prj->lastError ;
  236.           break;
  237.       
  238.         case BC_UseConf:
  239.           SaveEnv(prj, FALSE) ;
  240.           if (prj->lastError) extmsg->error = prj->lastError ;
  241.           break;
  242.       
  243.         case BC_SetWindow:
  244.           setWindow(prj) ;
  245.           extmsg->error = OK ;
  246.           break;
  247.         
  248.         default:
  249.           break;
  250.       }
  251.       if (extmsg) ReplyMsg((struct Message*)extmsg) ;
  252.     }
  253.     if (mask & (1<<(prj->notifyPort->mp_SigBit))) {
  254.       // attention fermeture du WB
  255.       if (partialClose(prj)) partialOpen(prj) ;
  256.     }
  257.     if (mask & 1<<(prj->win->UserPort->mp_SigBit)) {
  258.       /* recu message d'intuition
  259.        */
  260.       while((msg = GT_GetIMsg(prj->win->UserPort)) != NULL) {
  261.         /* retourne le message
  262.          */
  263.         class = msg->Class ;
  264.         ReplyMsg((struct Message*)msg) ;
  265.         if(class == IDCMP_CHANGEWINDOW) {
  266.           
  267.           RemoveGList(prj->win, &szgdg, 2) ;
  268.           szgdg.Width = prj->win->Width ;
  269.           tagdg.LeftEdge = prj->win->Width -10 ;
  270.           tagdg.TopEdge = prj->win->Height -10 ;
  271.           prj->backWin.width = prj->win->Width ;
  272.           prj->backWin.height = prj->win->Height ;      
  273.           prj->backWin.posX = prj->win->LeftEdge ;
  274.           prj->backWin.posY = prj->win->TopEdge ;
  275.           setWindow(prj) ;
  276.           reinit_win(prj) ; 
  277.           AddGList(prj->win, &szgdg, 0, 2, NULL) ;
  278.           RefreshGList(&szgdg, prj->win, NULL, -1) ;
  279.  
  280.           SaveEnv(prj, TRUE) ;
  281.         }  
  282.       }          
  283.     }
  284.     if (CheckIO((struct IORequest*)prj->treq)) {
  285.       WaitIO((struct IORequest*)prj->treq) ;
  286.       AbortIO((struct IORequest*)prj->treq) ;
  287.       WaitIO((struct IORequest*)prj->treq) ;
  288.       runtimer(prj) ;
  289.       getDate(prj) ;
  290.       effacer(prj) ;
  291.       retracer(prj) ;
  292.       //WindowToBack(prj->win) ;
  293.     }  
  294.  
  295.   }
  296.   AbortIO((struct IORequest*)prj->treq) ;
  297.   WaitIO((struct IORequest*)prj->treq) ;
  298.   RemPort(extPort) ;
  299.   FreeVec(extPort->mp_Node.ln_Name) ;
  300.   DeleteMsgPort(extPort) ;
  301.      
  302. }
  303.  
  304. void ez_req(UBYTE * title, UBYTE * body, UBYTE * gadg, APTR arglist ) {
  305.   struct EasyStruct * ez_rq ;
  306.   
  307.   if (ez_rq = AllocVec(sizeof(struct EasyStruct), MEMF_PUBLIC)) {
  308.     if (ez_rq->es_Title = AllocVec(strlen(title)+1, MEMF_PUBLIC)) {
  309.      strcpy(ez_rq->es_Title, title) ;
  310.      if (ez_rq->es_TextFormat = AllocVec(strlen(body)+1, MEMF_PUBLIC)) {
  311.       strcpy(ez_rq->es_TextFormat, body) ;
  312.       if (ez_rq->es_GadgetFormat = AllocVec(strlen(gadg)+1, MEMF_PUBLIC)) {
  313.        strcpy(ez_rq->es_GadgetFormat, gadg) ;
  314.        ez_rq->es_StructSize = sizeof(EasyStruct) ;
  315.        ez_rq->es_Flags = NULL ;
  316.        EasyRequestArgs(NULL, ez_rq, NULL, arglist) ;
  317.        FreeVec(ez_rq->es_GadgetFormat) ;
  318.       }
  319.       FreeVec(ez_rq->es_TextFormat) ;
  320.      }
  321.      FreeVec(ez_rq->es_Title) ;
  322.     }
  323.     FreeVec(ez_rq) ;
  324.   }
  325. }
  326.  
  327.